package com.cn.kehong.controller;


import com.cn.kehong.api.*;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/submission")
public class DataSubmissionController {

    @Autowired
    private IDataSubmissionService  dataSubmissionService;
    @Autowired
    private IDataSubmissionYyService dataSubmissionYyService;
    @Autowired
    private IDataSubmissionScService dataSubmissionScService;
    @Autowired
    private IDataSubmissionCcService dataSubmissionCcService;

    @Autowired
    private IDataSubmissionKbService dataSubmissionKbService;

    @RequestMapping("/getDataSubmissionList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<DataSubmission>> getDataSubmissionList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<DataSubmission>> response = new JsonResponse<List<DataSubmission>>();
        List<DataSubmission> list  = dataSubmissionService.getDataSubmissionList(queryForm);
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }

    @RequestMapping("/getDataSubmissionVoList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<DataSubmissionVo>> getDataSubmissionVoList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<DataSubmissionVo>> response = new JsonResponse<List<DataSubmissionVo>>();
        List<DataSubmissionVo> list  = dataSubmissionCcService.getDataSubmissionVoList(queryForm);
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }


    @ResponseBody
    @RequestMapping("/addDataSubmission/{frontToken}")
    public JsonResponse<String> addDataSubmission(@RequestBody DataSubmission dataSubmission, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = dataSubmissionService.addDataSubmission(dataSubmission);
        return CommonUtil.getResponseData(response,result);
    }


    @ResponseBody
    @RequestMapping("/updateDataSubmission/{frontToken}")
    public JsonResponse<String> updateDataSubmission(@RequestBody DataSubmission dataSubmission, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = dataSubmissionService.updateDataSubmission(dataSubmission);
        return CommonUtil.getResponseData(response,result);
    }



    @ResponseBody
    @RequestMapping("/deleteDataSubmissionById/{frontToken}")
    public JsonResponse<String> deleteDataSubmissionById(String id, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = dataSubmissionService.deleteDataSubmissionById(id);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/getDataSubmissionDetailById/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Object>> getDataSubmissionDetailById(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<Object>> response = new JsonResponse<List<Object>>();
        String id = queryForm.getId();
        String dataType = queryForm.getType();
        List<Object> list  = Lists.newArrayList();
        if(StringUtils.equals("1",dataType)||StringUtils.equals("10",dataType)||StringUtils.equals("11",dataType)||StringUtils.equals("12",dataType)||StringUtils.equals("13",dataType)){
            List<DataSubmissionYy> dataSubmissionYys = dataSubmissionYyService.getDataSubmissionYyListById(id);
            list.addAll(dataSubmissionYys);
        }else if(StringUtils.equals("2",dataType)){
            List<DataSubmissionSc> dataSubmissionScs = dataSubmissionScService.getDataSubmissionScListById(id);
            list.addAll(dataSubmissionScs);
        }else if (StringUtils.equals("4",dataType)){
            List<DataSubmissionCc> dataSubmissionCcs = dataSubmissionCcService.getDataSubmissionCcListById(id);
            list.addAll(dataSubmissionCcs);
        }else if (StringUtils.equals("7", dataType) || StringUtils.equals("8", dataType) || StringUtils.equals("9", dataType)) {
            List<DataSubmissionKb> dataSubmissionKbs = dataSubmissionKbService.getDataSubmissionKbListById(id, dataType);
            list.addAll(dataSubmissionKbs);
        }
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }



    @RequestMapping("/getDataSubmissionVoListByKb/{frontToken}")
    @ResponseBody
    public JsonResponse<List<DataSubmissionVo>> getDataSubmissionVoListByKb(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<DataSubmissionVo>> response = new JsonResponse<List<DataSubmissionVo>>();
        List<DataSubmissionVo> list  = dataSubmissionKbService.getDataSubmissionVoListByKb(queryForm);
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }


    @ResponseBody
    @RequestMapping("/addDataSubmissionByKb/{frontToken}")
    public JsonResponse<String> addDataSubmissionByKb(@RequestBody DataSubmission dataSubmission, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = dataSubmissionService.addDataSubmissionByKb(dataSubmission);
        return CommonUtil.getResponseData(response,result);
    }


    @ResponseBody
    @RequestMapping("/updateDataSubmissionByKb/{frontToken}")
    public JsonResponse<String> updateDataSubmissionByKb(@RequestBody DataSubmission dataSubmission, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = dataSubmissionService.updateDataSubmissionByKb(dataSubmission);
        return CommonUtil.getResponseData(response,result);
    }

}
